@extends('admin.common.admin_main')
@section('container')
<div class="uc-body">
    <div class="box-wide">
        <div class="box-thin bg-gray">
            <div id="viewForm" class="form">
                <div class="form-head">
                    <div class="tit">基础信息</div>
                    <a id="edit" class="edit">编辑</a>
                </div>
                <div class="form-body">
                    <div class="row">
                        <div class="label">登录账号：</div>
                        <div class="control-text">
                            <div class="static"><b>{{$account_info['user_phone']}}</b></div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">联系电话：</div>
                        <div class="control-text">
                            <div class="static"><b>{{$account_info['contact_phone']}}</b></div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">电子邮箱：</div>
                        <div class="control-text">
                            <div class="static"><b>{{$account_info['email']?$account_info['email']:""}}</b></div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">所在地：</div>
                        <div class="control-text">
                            <div class="static"><b>{{$p?$getNameList[$p]['name'].$getNameList[$c]['name'].$getNameList[$a]['name'].$address:""}}</b></div>
                        </div>
                    </div>
                </div>
                {{-- <div class="form-foot">
                    <div class="info"><span class="require">*</span> 如果你想获得更多的服务，需要完成认证才行，立即去 <a class="link" href="/user/company">认证！</a></div>
                </div> --}}
            </div>
            <div id="editForm" class="form" style="display: none">
                <div class="form-head">
                    <div class="tit">基础信息</div>
                </div>
                <div class="form-body">
                    <div class="row">
                        <div class="label">登录账号：</div>
                        <div class="control-text">
                            <div class="static">
                                <div class="static"><b>{{$account_info['user_phone']}}</b></div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">联系电话：</div>
                        <div class="control-text">
                            <div class="static">
                                <input class="form-control" id="phone" type="text" maxlength="11"
                                       placeholder="请输入您的手机号码" value="{{$account_info['contact_phone']}}">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">电子邮箱：</div>
                        <div class="control-text">
                            <div class="static">
                                <input class="form-control" id="email" type="text" placeholder="请输入您的电子邮箱" value="{{$account_info['email']}}">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">所在地：</div>
                        <div class="control-text">
                            <div class="static">
                                <select class="form-control j_linkage" id="province_change">
                                    <option value="">请选择</option>
                                    @forelse ($province_list as $key=>$value)
                                        <option value="{{$value['id']}}" {{$p?($p==$value['id']?"selected=selected":""):""}}>{{$value['name']}}</option>
                                    @empty
                                    @endforelse
                                </select>
                                @if ($p)
                                    <select class="form-control j_linkage" id="city_change">
                                        @forelse ($city_list as $key=>$value)
                                            <option value="{{$value['id']}}" {{$c==$value['id']?"selected=selected":""}}>{{$value['name']}}</option>
                                        @empty
                                        @endforelse
                                    </select>
                                @else
                                    <select class="form-control j_linkage" id="city_change">
                                        <option value="">请选择</option>
                                    </select>
                                @endif

                                @if ($p)
                                    <select class="form-control j_linkage" id="area_change">
                                        @forelse ($area_list as $key=>$value)
                                            <option value="{{$value['id']}}" {{$a==$value['id']?"selected=selected":""}}>{{$value['name']}}</option>
                                        @empty
                                        @endforelse
                                    </select>
                                @else
                                    <select class="form-control j_linkage" id="area_change">
                                        <option value="">请选择</option>
                                    </select>
                                @endif
                                <input class="form-control form-control-lg" id="address" type="text" placeholder="请输入您的详细地址" value="{{$address}}">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="label">&nbsp;</div>
                        <div class="control-text">
                            <a id="submit" class="btn btn-warning">保存</a>
                            <a id="cancel" class="btn btn-primary">取消</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('script')
<script>
    (function ($) {
        $(function () {
            //地区联动
            $(".j_linkage").change(function () {
                var $t = $(this);
                var id = $t.val();
                if (id && $t.next(".j_linkage").length > 0) {
                    $t.nextAll(".j_linkage").html('<option value="">请选择</option>');
                    getArea(id, $t.next());
                }
            });
            function getArea(id, $p) {
                $.ajax({
                    url: '/select_change',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        id: id
                    },
                    type: 'post',
                    dateType: 'json',
                    success: function (data) {
                        var result = JSON.parse(data);
                        var optHtml = '<option value="">请选择</option>';
                        for (var i = 0, l = result.length; i < l; i++) {
                            optHtml += '<option value="' + result[i].id + '">' + result[i].name + '</option>';
                        }
                        $p.html(optHtml);
                    }
                })
            }

            //编辑
            $("#edit").click(function () {
                $("#viewForm").css("display", "none");
                $("#editForm").css("display", "block");
            });
            //取消
            $("#cancel").click(function () {
                $("#viewForm").css("display", "block");
                $("#editForm").css("display", "none");
            });
            //保存
            $("#submit").click(function () {
                var $p = $("#phone");
                var phone = $p.val();
                if (!isPhone(phone)) {
                    return showErr($p, "请输入正确的手机号码！");
                }
                var $e = $("#email");
                var email = $e.val();
                if (!isEmail(email)) {
                    return showErr($e, "请输入正确的邮箱地址！");
                }
                var $s = $(".j_linkage");
                var province = $s.eq(0).val();
                if (!province) {
                    return showErr($s.eq(0), "请选择省！");
                }
                var city = $s.eq(1).val();
                if (!city) {
                    return showErr($s.eq(1), "请选择市！");
                }
                var district = $s.eq(2).val();
                if (!district) {
                    return showErr($s.eq(2), "请选择区！");
                }
                var area_list = province + "," + city + "," + district;
                var $a = $("#address");
                var address = $a.val();
                if (!address) {
                    return showErr($a, "请输入详细地址！");
                }
                var $btn = $(this);
                $.ajax({
                    url: "/user/action",
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        contact_phone: phone,
                        contact_email: email,
                        contact_address_id: area_list,
                        contact_address: address,
                        action:'update_user_base_info'
                    },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        var result = data["result"];
                        var msg = data["msg"];
                        if (result === 0) {
                            // $.Alert({
                            //     obj: $btn, txt: '修改成功！', callback: function () {
                            //         $("#j_alert").remove();
                            //         $("#cancel").click();
                            //     }
                            // });
                            window.location.href='/user/account';
                            return false;
                        } else {
                            return showErr($btn, msg);
                        }
                    },
                    error: function (data) {
                        return showErr($btn, "保存失败！");
                    }
                });
            });
        });
    })(jQuery);
</script>
@endsection
